<?php

	class AuthenticateController extends Zend_Controller_Action {
		
		public function init() {
			
			$am = new Model_AuthenticationManager();
			$user = $am->getlogguedInformations();
			
			$this->view->action_name = $this->getRequest()->getActionName();
			$this->view->controller_name = $this->getRequest()->getControllername();
		
			// Si loggué
			if($am->isLoggued()){
				$this->view->is_logged = true;
				
			// Si l'on est pas loggué
			} else {
				$this->view->is_logged = false;
			}
			
			$this->view->headLink()
					->appendStylesheet($this->_request->getBaseUrl().'/css/commun.css')
					->appendStylesheet($this->_request->getBaseUrl().'/css/index.css');
					
			$this->view->jQuery()
					->addJavascriptFile($this->view->baseUrl('/js/jquery/jquery.js'))
					->addJavascriptFile($this->view->baseUrl('/js/jquery/jquery.form.js'))
					->addJavascriptFile($this->view->baseUrl('/js/jquery/jquery.validation.js'))
					->addJavascriptFile($this->view->baseUrl('/js/jquery/jquery.validation.rules_custom.js'))
					->addJavascriptFile($this->view->baseUrl('/js/jquery/jquery.validation.messages_fr.js'));
			
			if (Zend_Registry::isRegistered('clientserver')) {
				$this->_clientserver = Zend_Registry::get('clientserver');
			
				$this->_apikey = $this->_clientserver->richcore->key;
				$this->_annuaire = new RichCongress_Rest_Service_RichAnnuaire_Client($this->_clientserver->richannuaire->uri);
				$this->_annuaire->setApiKey($this->_apikey);
				
	        } else {
	        	throw new Zend_Exception('[ Model_Authentication_Richcore ] unable to find your client server...');
	        }
		}
		
		public function indexAction() {
			
			$this->view->headTitle('APEL Workflow - Connexion');
			
			$send = $this->getRequest()->getParam('login_send');
			if(!is_null($send)) {
				
				//$authentication = new Model_Authentication($method = $this->_annuaire->authentication->method);
				$aM = new Model_AuthenticationManager();
				
				//authenticate user
				$login = $this->getRequest()->getParam('login');
				$password = $this->getRequest()->getParam('password');
				
				$this->view->login = $login;
				
				if(empty($login) or empty($password)) {
					$this->view->error = 'Erreur : Veuillez remplir tous les champs obligatoires.';
					return;
				}
				$isLogged = $aM->login($login, $password);
				
				// If not logged
				if(!$isLogged) {
					$this->view->error = 'Erreur : Veuillez vérifier votre login et/ou votre mot de passe';
					return;
				}
				
				// Redirect to home
				$redirector = $this->_helper->getHelper('Redirector');
				$redirector->gotoRoute(array('module' => 'frontend', 'controller' => 'index', 'action' => 'index'));
			}
			
		}
		
		public function logoutAction() {
			
			$aM = new Model_AuthenticationManager();
			$aM->logout();
			
			$redirector = $this->_helper->getHelper('Redirector');
			$redirector->gotoRoute(array('module' => 'frontend', 'controller' => 'index', 'action' => 'index'));
		}
		
	}
	
?>
